package com.zl.rbac.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zl.rbac.VO.ResultVO;
import com.zl.rbac.entity.Permission;
import com.zl.rbac.service.IPermissionService;
import com.zl.rbac.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 权限的控制器
 * @ClassName: PermissionController
 * @author: suke
 * @since: 2021/1/11 15:04
 */
@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    //加载权限
    @PostMapping("reload")
    @ResponseBody
    @RequiredPermission("加载权限")
    public ResultVO<String>  load() throws Exception{
        permissionService.load();
        return ResultVO.SUCCESS();
    }


    @GetMapping("list")
    @RequiredPermission("查询权限列表")
    public String  list(@RequestParam(defaultValue = "1") Integer pageIndex, Model model) throws Exception{
        Page<Permission> page = permissionService.queryByPage(pageIndex,1);
        model.addAttribute("page",page);
        return "permission/list";  // 逻辑视图名:  经过视图解析器: classpath:templates/permission/list.html
    }


    @PostMapping("delete")
    @ResponseBody
    @RequiredPermission("删除权限")
    public ResultVO<String>  delete(Integer id) throws Exception{
        permissionService.delete(id);
        return ResultVO.SUCCESS();
    }
}
